package stu.day19_day24.day20;

import java.util.Arrays;

public class LeetCode135 {
    public int candy(int[] ratings) {


        int[] left = new int[ratings.length];
        int[] right = new int[ratings.length];
        int[] top = new int[ratings.length];

        left[0] = 1;
        right[ratings.length - 1] = 1;

        for(int i = 1; i < ratings.length; i++) {
            if(ratings[i] > ratings[i-1]) {
                left[i] = left[i-1] + 1;
            }else {
                left[i] = 1;
            }
        }

        for(int i = ratings.length - 2; i >= 0; i--) {
            if(ratings[i] > ratings[i+1]) {
                right[i] = right[i+1] + 1;
            }else {
                right[i] = 1;
            }
        }

        for(int i = 0; i<ratings.length ; i++ ) {
            top[i] = left[i]>right[i]?left[i]:right[i];

        }

        return Arrays.stream(top).reduce(0,Integer::sum);


    }


}
